const Throttle = require("../template")

function testThrottleBasic() {
  console.log(
    "1. 基本节流功能测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttle(() => {
        callCount++
      }, 100)
      
      throttled()
      throttled()
      throttled()
      
      return callCount === 1 // 节流应该调用一次
    })()
  )

  console.log(
    "2. 立即执行节流测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttleImmediate(() => {
        callCount++
      }, 100, true)
      
      throttled()
      throttled()
      throttled()
      
      return callCount === 1 // 立即执行应该调用一次
    })()
  )

  console.log(
    "3. 最大等待时间节流测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttleMaxWait(() => {
        callCount++
      }, 100, 300)
      
      throttled()
      throttled()
      throttled()
      
      return callCount === 1 // 节流应该调用一次
    })()
  )

  console.log(
    "4. 可取消节流测试:",
    (() => {
      let callCount = 0
      const { throttled, cancel } = Throttle.throttleWithCancel(() => {
        callCount++
      }, 100)
      
      throttled()
      throttled()
      cancel()
      
      return callCount === 1 // 取消前应该调用一次
    })()
  )

  console.log(
    "5. 可刷新节流测试:",
    (() => {
      let callCount = 0
      const { throttled, flush } = Throttle.throttleWithFlush(() => {
        callCount++
      }, 100)
      
      throttled()
      throttled()
      flush()
      
      return callCount === 2 // 刷新应该再调用一次
    })()
  )

  console.log(
    "6. 状态查询节流测试:",
    (() => {
      let callCount = 0
      const { throttled, isPending, isCancelled } = Throttle.throttleWithState(() => {
        callCount++
      }, 100)
      
      throttled()
      
      return isPending() === false && isCancelled() === false
    })()
  )

  console.log(
    "7. 条件执行节流测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttleWithCondition(() => {
        callCount++
      }, 100, (args) => args[0] > 5)
      
      throttled(3) // 不满足条件
      throttled(7) // 满足条件
      throttled(2) // 不满足条件
      
      return callCount === 1 // 满足条件应该调用一次
    })()
  )

  console.log(
    "8. 优先级节流测试:",
    (() => {
      let callCount = 0
      const highPriorityThrottled = Throttle.throttleWithPriority(() => {
        callCount++
      }, 100, 10)
      
      const lowPriorityThrottled = Throttle.throttleWithPriority(() => {
        callCount++
      }, 100, 1)
      
      lowPriorityThrottled()
      highPriorityThrottled()
      
      return callCount === 2 // 两个都应该调用
    })()
  )

  console.log(
    "9. 批量执行节流测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttleWithBatch(() => {
        callCount++
      }, 100, 3)
      
      throttled()
      throttled()
      throttled()
      
      return callCount === 1 // 批量执行应该调用一次
    })()
  )

  console.log(
    "10. 错误处理节流测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttleWithErrorHandling(() => {
        callCount++
        throw new Error('测试错误')
      }, 100, (error) => {
        // 错误处理
      })
      
      throttled()
      
      return callCount === 1 // 应该调用一次
    })()
  )

  console.log(
    "11. 进度回调节流测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttleWithProgress(() => {
        callCount++
      }, 100, (progress) => {
        // 进度回调
      })
      
      throttled()
      throttled()
      throttled()
      
      return callCount === 1 // 节流应该调用一次
    })()
  )

  console.log(
    "12. 统计功能节流测试:",
    (() => {
      let callCount = 0
      const { throttled, getStats } = Throttle.throttleWithStats(() => {
        callCount++
      }, 100)
      
      throttled()
      throttled()
      throttled()
      
      const stats = getStats()
      return typeof stats === 'object' && callCount === 1
    })()
  )

  console.log(
    "13. this上下文节流测试:",
    (() => {
      const throttleObj = {
        multiplier: 2,
        processThrottle: function() {
          const throttled = Throttle.throttle(() => {
            return this.multiplier
          }, 100)
          return throttled()
        }
      }
      return throttleObj.processThrottle() === 2 // 节流应该调用一次
    })()
  )

  console.log(
    "14. 复杂对象this绑定节流测试:",
    (() => {
      const throttleConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function() {
          const throttled = Throttle.throttle(() => {
            return this.config.threshold
          }, 100)
          return throttled()
        }
      }
      return throttleConfigObj.processWithConfig() === 2 // 节流应该调用一次
    })()
  )
}

testThrottleBasic()
